<template>
  <BasicModal
    v-bind="$attrs"
    :title="isUpdate ? t('common.edit') : t('common.create')"
    @register="register"
    @ok="handleSubmit"
    :destroyOnClose="true"
    :minHeight="500"
    :width="600"
  >
    <BasicForm @register="registerForm" />
  </BasicModal>
</template>

<script lang="ts" setup>
  import { ref } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form';
  import { getDocSettingForm } from './data';

  import { getFileTypeTable, addFileTypeTable, updateDocTypeBase } from '/@/api/demo/system';
  import { useMessage } from '/@/hooks/web/useMessage';

  const isUpdate = ref<boolean>(false);
  const { t } = useI18n();
  const { createMessage } = useMessage();
  const emit = defineEmits(['success', 'register']);

  // 提交
  const handleSubmit = async () => {
    const values = await validate();
    const newArr = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
    newArr[7] = values.check1 ? 1 : 0;
    newArr[6] = values.check2 ? 1 : 0;
    newArr[2] = values.check3 ? 1 : 0;
    newArr[3] = values.check4 ? 1 : 0;
    newArr[1] = values.check5 ? 1 : 0;
    newArr[0] = values.check6 ? 1 : 0;

    const params = {
      oId: values.oId,
      name: values.name,
      template: 'none',
      counterID: values.counterID,
      createSoft: values.createSoft,
      datavault: values.datavault,
      dataType: newArr.join(''),
    };
    if (isUpdate.value) {
      // 修改
      try {
        setModalProps({ confirmLoading: true });
        await updateDocTypeBase(params);
        emit('success');
        createMessage.success(t('common.saveSuccess'));
        closeModal();
      } finally {
        setModalProps({ confirmLoading: false });
      }
    } else {
      try {
        setModalProps({ confirmLoading: true });
        await addFileTypeTable(params);
        emit('success');
        createMessage.success(t('common.saveSuccess'));
        closeModal();
      } finally {
        setModalProps({ confirmLoading: false });
      }
    }
  };

  // 初始化弹窗
  const [register, { setModalProps, closeModal }] = useModalInner((data) => {
    setModalProps({ confirmLoading: false });
    isUpdate.value = !!data?.isUpdate;
    if (isUpdate.value) {
      getFileTypeTable(data.currentRecord.id).then((res) => {
        const params = {
          oId: res.docMaindto.oId,
          name: res.docMaindto.name,
          template: 'none',
          counterID: res.docMaindto.counterID === '' ? null : res.docMaindto.counterID,
          createSoft: res.docMaindto.createSoft,
          datavault: res.docMaindto.datavault === 'pdmBinVault' ? '' : res.docMaindto.datavault,
          check1: res.docMaindto.dataType.split('')[7] == 1 ? true : false,
          check2: res.docMaindto.dataType.split('')[6] == 1 ? true : false,
          check3: res.docMaindto.dataType.split('')[2] == 1 ? true : false,
          check4: res.docMaindto.dataType.split('')[3] == 1 ? true : false,
          check5: res.docMaindto.dataType.split('')[1] == 1 ? true : false,
          check6: res.docMaindto.dataType.split('')[0] == 1 ? true : false,
          sourceFileStorageWay:
            res.docMaindto.datavault === '' || res.docMaindto.datavault === 'pdmBinVault'
              ? '0'
              : '1',
        };
        setFieldsValue(params);
      });
      updateSchema([
        { field: 'check1', componentProps: { disabled: true } },
        { field: 'check2', componentProps: { disabled: true } },
        { field: 'check3', componentProps: { disabled: true } },
        { field: 'check4', componentProps: { disabled: true } },
      ]);
    }
  });

  // 属性详情
  const [registerForm, { validate, setFieldsValue, updateSchema }] = useForm({
    labelCol: { span: 6 },
    wrapperCol: { span: 16 },
    schemas: getDocSettingForm(isUpdate),
    showResetButton: false,
    showSubmitButton: false,
  });
</script>
